package org.example.controller.usercontroller;


import lombok.extern.slf4j.Slf4j;
import org.example.dto.UserDto2;
import org.example.dto.UserLoginDto;
import org.example.entity.Condition;
import org.example.entity.User;
import org.example.vo.UserVO;
import org.example.utils.Result;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j

public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param
     * @return
     * TODO用户名密码加密
     */
    @PostMapping("/login")
    public Result<UserVO> login(@RequestBody UserDto2 userDto2){
        UserLoginDto userLoginDto = new UserLoginDto(userDto2.getUsername(), userDto2.getPassword());
        log.info("用户登录：{}", userLoginDto);
        UserVO userVO =userService.login(userLoginDto);
        if (userVO == null) {
            return Result.error("用户名或密码错误");
        }
        return Result.success(userVO);
    }

    /**
     * 用户注册
     * @param
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody UserDto2 userDto2){
        UserLoginDto userLoginDto = new UserLoginDto(userDto2.getUsername(), userDto2.getPassword());
        log.info("用户注册：{}", userLoginDto);
        Boolean flag =userService.register(userLoginDto);
        if (!flag) {
            return Result.error("用户名已存在");
        }
        return Result.success();
    }
    /**
     * 完善用户信息
     */
    @PostMapping("/completeUserInfo")
    public Result completeUserInfo(@RequestBody UserVO userVO){

        log.info("完善用户信息：{}", userVO);
        Boolean b = userService.perfectInformation(userVO);
        if (!b) {
            return Result.error("操作失败");
        }
        return Result.success("操作成功");
    }
    /**
     * 获取所有教师信息
     */
    @GetMapping("/teacher")
    public Result<List<User>> getUsers(){
        log.info("获取所有用户信息");
        List<User>  users= userService.getuser();
        return Result.success(users);
    }
    /**
     * 获取符合条件的用户信息
     */

    @GetMapping("/getUserInfo")
    public Result<List<User>> getUserInfo(@RequestBody Condition condition){
        log.info("获取符合条件的用户信息：{}", condition);
        List<User> users = userService.getUserInfo(condition);
        return Result.success(users);
    }
}
